#include "regi.h"
#include "ui_regi.h"

regi::regi(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::regi)
{
    ui->setupUi(this);
}

regi::regi(QTcpSocket* s, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::regi)
{
    ui->setupUi(this);
    UIDesign::setButtonUI(ui->backButton);
    UIDesign::setButtonUI(ui->confirmButton);
    UIDesign::setLineEditUI(ui->userName);
    UIDesign::setLineEditUI(ui->password);
    UIDesign::setLineEditUI(ui->passAgain);
    UIDesign::setTransparent(ui->label);
    UIDesign::setTransparent(ui->label_3);
    UIDesign::setTransparent(ui->label_4);

    UIDesign::setWindowUI(ui->centralwidget);

    setWindowTitle("注册");
    setWindowOpacity(0.95);
    setFixedSize(this->width(), this->height());

    sock = s;
    disconnect(s, SIGNAL(readyRead()), 0, 0);
}

regi::~regi()
{
    delete ui;
}

void regi::on_confirmButton_clicked()
{
    //从服务器自动分配一个id过来，待开发

    QString uName = ui->userName->text();
    QString pWordFirst = ui->password->text();
    QString pWordSecond = ui->passAgain->text();

    if(uName==""){
        //判断用户名为空的情况
        QMessageBox::information(this, "提示", "用户名不能为空");
    }
    else if(pWordFirst!=pWordSecond){
        //判断两次输入密码是否一致
        QMessageBox::information(this, "提示", "两次密码输入不统一，请重新输入");
        ui->password->setText("");
        ui->passAgain->setText("");
    }else if(pWordFirst.length()<6||pWordFirst.length()>16){
        QMessageBox::information(this, "提示", "密码长度不符合要求，请重新输入");
        ui->password->setText("");
        ui->passAgain->setText("");
    }else{
        disconnect(sock, SIGNAL(readyRead()), 0, 0);
        connect(sock, SIGNAL(readyRead()), this, SLOT(recv()));
        QJsonObject request;
        request.insert("order", 2);
        request.insert("myid", 0);
        QJsonObject data;
        data.insert("username", ui->userName->text());
        data.insert("pwd", ui->password->text());
        request.insert("data", data);
        qDebug() << "---------------------------------------";
        qDebug() << request;
        qDebug() << "data:\n";
        qDebug() << data;
        qDebug() << "---------------------------------------";
        QJsonDocument document;
        document.setObject(request);
        QByteArray byteRequest = document.toJson();
        sock->write(byteRequest);
    }
}

void regi::on_backButton_clicked()
{
    this->close();
    emit mainclose();
}




void regi::recv()
{
    QByteArray byteRequest;
    byteRequest = sock->readAll();
    QJsonParseError err;
    QJsonDocument document = QJsonDocument::fromJson(byteRequest, &err);
    if(err.error == QJsonParseError::NoError)
    {
        QJsonObject request = document.object();
        QJsonValue Rstatus = request.value("status");
        bool status = Rstatus.toBool();
        qDebug() << "---------------------------------------";
        qDebug() << request;
        if(status)
        {
            QJsonValue Rmesg = request.value("mesg");
            QJsonObject mesg = Rmesg.toObject();
            QJsonValue Rmyid = mesg.value("myid");
            QString myid = Rmyid.toString();
            qDebug() << "mesg:\n";
            qDebug() << mesg;
            qDebug() << "---------------------------------------";
            QMessageBox::information(this, "提示", "已注册。ID：" + myid);
            this->close();
            emit mainclose();;
        }
        else
        {
            qDebug() << "---------------------------------------";
            QMessageBox::warning(this, "警告", "申请不被认可！");
        }
    }
    else QMessageBox::warning(this, "警告", "服务器下传失败");
}
